package org.csu.mypetstore_ssm.controller;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.csu.mypetstore_ssm.domain.Account;
import org.csu.mypetstore_ssm.domain.Cart;
import org.csu.mypetstore_ssm.domain.Order;
import org.csu.mypetstore_ssm.service.AccountService;
import org.csu.mypetstore_ssm.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.csu.mypetstore_ssm.domain.Consignee;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.util.List;

@Controller
//@RequestMapping("/order")
public class OrderController {
    @Autowired
    HttpSession httpSession;
    @Autowired
    AccountService accountService;
    @Autowired
    OrderService orderService;
    private Account account;
    private Order order;
    private Cart cart;

    @PostMapping("/confirmOrder")
    public String confirmOrder(Model model){
        Order order = (Order)httpSession.getAttribute("order");
//        Account account = (Account)httpSession.getAttribute("account");
        model.addAttribute("order",order);
        return "order/confirmOrder";
    }

    @PostMapping("/success")
    public String success(Model model){
        Order order = (Order)httpSession.getAttribute("order");
//        Account account = (Account)httpSession.getAttribute("account");
        model.addAttribute("order",order);
        orderService.saveOrder(order);
        return "order/confirmOrder";
    }

    @GetMapping("/listOrder")
    public String listOrder(){
        return "order/listOrders";
    }

    @GetMapping("/newOrder")
    public String newOrder(HttpServletRequest request) {
        HttpSession session = request.getSession();
        account = (Account) session.getAttribute("loginAccount");
        cart = (Cart) session.getAttribute("cart");

        if (account == null || cart == null) {
            return "redirect:/error"; // 如果用户未登录或购物车为空，跳转到错误页面
        }

        String username = account.getUsername();
        List<Consignee> consigneeList = accountService.getConsignee(username);
        session.setAttribute("consigneeList", consigneeList);

        // 创建新的订单对象
        order = new Order();
        order.initOrder(account, cart);
        session.setAttribute("order", order); // 将新订单存入 Session

        orderService.placeOrder(cart);

        return "order/newOrders";
    }

    @GetMapping("/shippingForm")
    public String shippingForm(){
        return "order/shippingForm";
    }

    @GetMapping("/viewOrder")
    public String viewOrder(Model model){
        Order order=(Order)httpSession.getAttribute("order");
        model.addAttribute("order", order);
        return "order/viewOrder";
    }
}
